﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Red.Controls.Converters
{
    // Value converter for adding/subtracting integer values to the binded value
    public class AddIntValueConverter : IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, CultureInfo cultureInfo )
        {
            if ( value == null )
            {
                return null;
            }

            object result = value;
            int parameterValue;

            if ( targetType == typeof( Int32 ) )
            {
                if ( int.TryParse( (string)parameter, NumberStyles.Integer, cultureInfo, out parameterValue ) )
                {
                    result = (int)value + parameterValue;
                }
            }
            else if ( targetType == typeof( Double ) )
            {
                double paramDouble;
                if ( double.TryParse( (string)parameter, NumberStyles.Any, cultureInfo, out paramDouble ) )
                {
                    parameterValue = (int)paramDouble;
                    result = (int)value + parameterValue;
                }
            }

            return result;
        }

        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            throw new NotImplementedException();
        }
    }
}
